# -*- coding: utf-8 -*-
import scrapy

import json
import time
from pprint import pprint
from copy import deepcopy, copy
from FDC_spider.utils.transfer_time import utc_to_local
from FDC_spider.items import FdcEstateGuidItem, FdcBuildingItem, FdcRoomItem


class XiamenSpider(scrapy.Spider):
    name = 'xiamen'
    allowed_domains = ['xm.gov.cn', '222.76.242.144']
    start_urls = ['http://222.76.242.144:8081/home/Getzslp']
    estate_url = 'http://222.76.242.144:8081/home/Getzslpmx'  # POST    楼栋url
    room_li_url = 'http://222.76.242.144:8081/Lp/LPPartial'  # POST    房屋列表url

    def start_requests(self):
        # 构造初始请求参数
        data_dict = dict(
            currentpage=str(1),
            pagesize=str(20),
            searchtj='',
            orderby='',
        )
        yield scrapy.FormRequest(
            XiamenSpider.start_urls[0],
            formdata=data_dict,
        )

    def parse(self, response):
        """
        获取楼盘列表信息
        :param response:
        :return:
        """
        resp_dict = json.loads(response.body.decode())
        body_dict = json.loads(resp_dict['Body'])
        item_li = body_dict['bodylist']
        total_num = body_dict['recordcount']
        page_size = body_dict['pagesize']
        current_page_num = body_dict['currentpage']
        total_page = int(total_num) // int(page_size) if int(total_num) % int(page_size) == 0 else int(
            total_num) // int(page_size) + 1
        # 翻页
        if int(current_page_num) < int(total_page):
            # 构造下一页请求参数
            next_data_dict = dict(
                currentpage=str(int(current_page_num) + 1),
                pagesize=str(20),
                searchtj='',
                orderby='',
            )
            yield scrapy.FormRequest(
                XiamenSpider.start_urls[0],
                formdata=next_data_dict,
                callback=self.parse,
            )

        # 获取楼盘信息
        for item_dict in item_li:
            item_eg = FdcEstateGuidItem()
            item_eg['projectId'] = item_dict.get('TRANSACTION_ID', None)
            item_eg['preSalePermit'] = item_dict.get('YSXKZH', None)
            item_eg['projectName'] = item_dict.get('XMMC', None)
            item_eg['estateAddress'] = item_dict.get('XMDZ', None)
            item_eg['districtName'] = item_dict.get('XMDQ', None)
            item_eg['approvalRoomNo'] = item_dict.get('PZTS', None)
            item_eg['approvalArea'] = item_dict.get('PZMJ', None)
            item_eg['approvalResidenceNum'] = item_dict.get('PZZZTS', None)
            item_eg['approvalResidenceArea'] = item_dict.get('PZZZMJ', None)
            item_eg['saleableResidenceNum'] = item_dict.get('KSZZTS', None)
            item_eg['saleableResidenceArea'] = item_dict.get('KSZZMJ', None)
            item_eg['saleableRoomNo'] = item_dict.get('KSTS', None)
            item_eg['saleableArea'] = item_dict.get('KSMJ', None)
            item_eg['certDate'] = utc_to_local(item_dict['GETDATE']) if item_dict.get('GETDATE', None) else None
            item_eg['approvalDate'] = utc_to_local(item_dict['APPROVALDATE']) if item_dict.get('APPROVALDATE',
                                                                                               None) else None
            data_dict = dict(
                tid=str(item_eg['projectId']),
            )
            yield scrapy.FormRequest(
                XiamenSpider.estate_url,
                formdata=data_dict,
                callback=self.parse_estate,
                meta={'item_eg': deepcopy(item_eg)},
            )

    def parse_estate(self, response):
        """
        获取楼盘信息
        :param response:
        :return:
        """
        item_eg = copy(response.meta['item_eg'])
        resp_dict = json.loads(response.body.decode())
        body_li = json.loads(resp_dict['Body'])
        item_eg['developerName'] = body_li[0]['KFSMC']
        yield item_eg

        # 楼栋url
        building_url_temp = 'http://222.76.242.144:8081/LP/Index?transactionid={}&projectName={}'
        item_bd = FdcBuildingItem()
        item_bd['buildingUrl'] = building_url_temp.format(item_eg['projectId'], item_eg['projectName'])
        item_bd['projectName'] = item_eg['projectName']
        item_bd['districtName'] = item_eg['districtName']
        item_bd['estateAddress'] = item_eg['estateAddress']
        item_bd['developerName'] = item_eg['developerName']
        yield scrapy.Request(
            item_bd['buildingUrl'],
            callback=self.parse_building,
            meta={'item_bd': deepcopy(item_bd)},
        )

    def parse_building(self, response):
        """
        获取楼栋信息
        :param response:
        :return:
        """
        item_bd = copy(response.meta['item_bd'])
        # 获取楼栋分组
        ul_li = response.xpath("//ul[@id='browser']/li/ul")
        for ul in ul_li:
            item_bd['blockName'] = ul.xpath("./li/span/text()").extract_first()
            yield item_bd

            # 获取房屋列表分组
            a_li = ul.xpath("./li/ul/li/a")
            for a in a_li:
                item_rm = FdcRoomItem()
                item_rm['projectName'] = item_bd['projectName']
                item_rm['districtName'] = item_bd['districtName']
                item_rm['blockName'] = item_bd['blockName']
                params_str = a.xpath("./@href").extract_first()
                params_li = params_str.split('(')[1].split(')')[0].split(',')
                # 构造POST请求参数
                data_dict = dict(
                    BuildID=params_li[0],
                    NAID=params_li[1],
                    lotid=params_li[2],
                    t=str(round(time.time() * 1000))
                )
                yield scrapy.FormRequest(
                    XiamenSpider.room_li_url,
                    formdata=data_dict,
                    callback=self.parse_room_li,
                    meta={'item_rm': deepcopy(item_rm)},
                )

    def parse_room_li(self, response):
        """
        获取房屋列表信息
        :param response:
        :return:
        """
        item_rm = copy(response.meta['item_rm'])
        # 获取房屋楼层分组
        tr_li = response.xpath("//table[@id='Table1']//tr/td[text()!='']/..")
        for tr in tr_li:
            item_rm['roomFloor'] = tr.xpath("./td[1]/text()").extract_first()
            # 获取当前楼层的房屋分组
            td_li = tr.xpath("./td[position()>1]")
            for td in td_li:
                if td.xpath("./text()"):
                    item_rm['roomNo'] = td.xpath("./text()").extract_first().strip()
                    item_rm['roomId'] = td.xpath("./@id").extract_first().strip()
                    # 房屋url
                    room_url_temp = 'http://222.76.242.144:8081/LP/Fwztxx?HouseId={}&yyxx=undefined&zs=undefined&zt={}'
                    class_str = td.xpath("./@class").extract_first()
                    item_rm['statusNo'] = 0 if class_str.startswith('fw_bksfw') else 1
                    yield scrapy.Request(
                        room_url_temp.format(item_rm['roomId'], item_rm['statusNo']),
                        callback=self.parse_room,
                        meta={'item_rm': deepcopy(item_rm)},
                    )

    def parse_room(self, response):
        """
        获取房屋信息
        :param response:
        :return:
        """
        item_rm = copy(response.meta['item_rm'])
        item_rm['roomLocation'] = response.xpath("//td[text()='坐落']/following-sibling::td[1]/text()").extract_first()
        item_rm['roomNature'] = response.xpath("//td[text()='性质']/following-sibling::td[1]/text()").extract_first()
        item_rm['roomUse'] = response.xpath("//td[text()='用途']/following-sibling::td[1]/text()").extract_first()
        item_rm['buildingArea'] = response.xpath("//td[text()='面积']/following-sibling::td[1]/text()").extract_first()
        item_rm['saleStatus'] = response.xpath("//td[text()='销售状态']/following-sibling::td[1]/text()").extract_first()
        item_rm['unitPrice'] = response.xpath("//td[text()='拟售价格']/following-sibling::td[1]/text()").extract_first()
        item_rm['stateOfOwnership'] = response.xpath(
            "//td[text()='权属状态']/following-sibling::td[1]/text()").extract_first()
        item_rm.pop('statusNo')
        yield item_rm
